﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpMUD.Framework.Services.Configuration;
using SharpMUD.Framework.Workflow.Generic;
using SharpMUD.Framework.ComponentModel;
using SharpMUD.Framework.Services.Threading;
using System.Threading.Tasks;

namespace SharpMUD.Framework.Services.Telnet
{
    [ServiceDependency(typeof(ITaskFactoryProviderService))]
	public partial class TelnetServer : IService {
        TaskFactory _TaskFactory;        

		#region IService Members

		public IServicesHost ServicesHost
		{
			get;
			private set;
		}

		public void Initialize (IServicesHost servicesHost)
		{
            if (this.ExecutionStatus != ExecutionStatusTokens.UnInitialized)
                throw new InvalidExecutionStatusTransitionException(this.ExecutionStatus, ExecutionStatusTokens.Initializing);
            this.ExecutionStatus = ExecutionStatusTokens.Initializing;
            log.Info(this.ExecutionStatus.ToString());

			this.ServicesHost = servicesHost;
            
            this.Configuration =
                (from p in this.ServicesHost.Services
                 where p is IConfigurationProvider
                 let f = (p as IConfigurationProvider).LoadConfiguration<TelnetServer, TelnetServerConfiguration>()
                    where f != null
                    select f).First();

            this.DetectApplicableNetworkFilterServices();

            _TaskFactory = this.ServicesHost.GetService<ITaskFactoryProviderService>().GetTaskFactory();

            this.SetupListeners();

			this.IsInitialized = true;
            this.ExecutionStatus = ExecutionStatusTokens.Initialized;
            log.Info(this.ExecutionStatus.ToString());
		}

		public bool IsInitialized
		{
			get;
			private set;
		}

		#endregion
	}
}
